﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Plc;
using Tecnomatix.Engineering.Utilities;

namespace autoCheck
{
    public class AutoCheck : TxButtonCommand
    {
        public override string Category
        {
            get
            {
                return "AutoCheck";
            }
        }

        public override string Name
        {
            get
            {
                return "提交答案";
            }
        }

        public override void Execute(object cmdParams)
        {
            
            var box = MessageBox.Show("请确保提交答案开始自动评分时所有信号、机构皆已复位，无强制状态，避免干扰自动评分。", "提示",MessageBoxButtons.YesNo);
            if (box == DialogResult.Yes)
            {
                Thread thread = new Thread(() => { CheckStart(); });
                thread.Start();
            }
        }
        public void CheckStart()
        {
            Dictionary<string, Object> requestData = new Dictionary<string, Object>();
            List<Dictionary<string, Object>> questionList = new List<Dictionary<string, Object>>();
            string[] key;
            string token;
            string questionId;
            string uploadGrade;
            try
            {
                key = File.ReadAllText("D://base//ProcessSimulation//key.txt").Split(',');
                token = key[0];
                questionId = key[1];
                uploadGrade = key[2];
            }
            catch (Exception)
            {
                MessageBox.Show("基础环境出现问题，请联系管理员，CODE:AC01");
                return;
            }
            //--------------------------------------
            //根据此模板编写判题逻辑
            CheckFirstStage(questionId, questionList);
            CheckSecondStage(questionId, questionList);
            CheckThirdStage(questionId, questionList);

            //--------------------------------------

            requestData.Add("token", token);
            requestData.Add("data", questionList);
            Dictionary<string, Object> answerPostRes;
            try
            {
                answerPostRes = HttpUtils.HttpPostNew(uploadGrade, requestData);
                MessageBox.Show(answerPostRes["msg"].ToString()); 
                if (answerPostRes == null || answerPostRes.Count == 0 || !answerPostRes["code"].ToString().Equals("0"))
                {
                    MessageBox.Show("答案上传出错，请联系管理员 CODE:AC09" + answerPostRes["code"]);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("答案上传出错，请联系管理员 CODE:AC08"+e.Message);
            }
        }


        private void CheckThirdStage(string questionId, List<Dictionary<string, object>> questionList)
        {
            StringBuilder answer = new StringBuilder();
            Dictionary<string, object> Stage = new Dictionary<string, object>();
            double score = 0;
            //---------------------
            TxObjectList ojectNameByFF01 = TxApplication.ActiveDocument.GetObjectsByName("FF01"); 
            if (ojectNameByFF01 != null)
            {
                foreach (ITxObject item in ojectNameByFF01)
                {
                    if ("Tecnomatix.Engineering.TxPlcGenericConveyor" == item.GetType().ToString())
                    {
                        
                        ITxPlcGenericConveyor FF01 = item as ITxPlcGenericConveyor;
                        try
                        {
                            if (FF01.PathCurve.Direction.ToString().Equals("1, 0, 0, -202\n0, 0, -1, -827.42\n0, 1, 0, 918.5\n0, 0, 0, 1") &&
                        FF01.PathCurve.EndPoint.ToString().Equals("1, 0, 0, -202\n0, 1, 0, -827.42\n0, 0, 1, 918.5\n0, 0, 0, 1") &&
                        FF01.PathCurve.StartPoint.ToString().Equals("1, 0, 0, -202\n0, 1, 0, -507.42\n0, 0, 1, 918.5\n0, 0, 0, 1"))
                            {
                                answer.AppendLine("传送带路径设置正确 +5分");
                                score += 5;
                            }
                            else
                            {
                                answer.AppendLine("传送带路径设置错误");
                            }
                        }
                        catch (Exception)
                        {
                            answer.AppendLine("传送带路径设置错误");
                        }
                        try
                        {
                            if (
                        FF01.MaxSpeed.Equals(200) &&
                        FF01.MovingSurfaceCollisionTolerance.Equals(5) &&
                        FF01.MovingSurfaceSensitivity.Equals(5) &&
                        FF01.IsSkidSurfaceType == true)
                            {
                                answer.AppendLine("FF01_Start 正确 +2.5分");
                                score += 5;
                            }
                            else
                            {
                                answer.AppendLine("FF01_Start 错误");
                            }
                        }
                        catch (Exception)
                        {
                            answer.AppendLine("FF01_Start 错误");
                        }
                        
                        TxObjectList objectNameByFF01_Start = TxApplication.ActiveDocument.GetObjectsByName("FF01_Start");
                        if (objectNameByFF01_Start != null)
                        {
                            foreach (ITxObject i in objectNameByFF01_Start)
                            {
                                try
                                {
                                    if ("Tecnomatix.Engineering.TxPlcOutputSignal" == i.GetType().ToString())
                                    {
                                        var ff01start = i as TxPlcOutputSignal;
                                        if (ff01start != null && ff01start.ConnectedLBEntries[0].Name.Equals("Start") && ff01start.IECFormatAddress.Equals("Q0.3"))
                                        {
                                            answer.AppendLine("FF01_Start 正确 +2.5分");
                                            score += 2.5;
                                        }
                                        else
                                        {
                                            answer.AppendLine("FF01_Start 错误");
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    answer.AppendLine("FF01_Start 错误");
                                }
                            }
                        }
                        TxObjectList objectNameByFF01_Stop = TxApplication.ActiveDocument.GetObjectsByName("FF01_Stop");
                        if (objectNameByFF01_Stop != null)
                        {
                            foreach (ITxObject i in objectNameByFF01_Stop)
                            {
                                try
                                {
                                    if ("Tecnomatix.Engineering.TxPlcOutputSignal" == i.GetType().ToString())
                                    {
                                        var ff01stop = i as TxPlcOutputSignal;
                                        if (ff01stop != null && ff01stop.ConnectedLBEntries[0].Name.Equals("Stop") && ff01stop.IECFormatAddress.Equals("Q0.4"))
                                        {
                                            answer.AppendLine("FF01_Stop 正确 +2.5分");
                                            score += 2.5;
                                        }
                                        else
                                        {
                                            answer.AppendLine("FF01_Stop 错误");
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    answer.AppendLine("FF01_Stop 错误");
                                }
                                      
                            }
                        }
                        if (CheckSignalAddressByName("ST01_mtp_UP", "Q0.1"))
                        {
                            answer.AppendLine("ST01_mtp_UP 正确 +5分");
                            score += 5;
                        }
                        else
                        {
                            answer.AppendLine("ST01_mtp_UP 错误");
                        }
                        if (CheckSignalAddressByName("ST01_mtp_DOWN", "Q0.2"))
                        {
                            answer.AppendLine("ST01_mtp_DOWN 正确 +5分");
                            score += 5;
                        }
                        else
                        {
                            answer.AppendLine("ST01_mtp_DOWN 错误");
                        }
                        if (CheckSignalAddressByName("ST01_at_UP", "I0.1"))
                        {
                            answer.AppendLine("ST01_at_UP 正确 +5分");
                            score += 5;
                        }
                        else
                        {
                            answer.AppendLine("ST01_at_UP 错误");
                        }
                        if (CheckSignalAddressByName("ST01_at_DOWN", "I0.2"))
                        {
                            answer.AppendLine("ST01_at_DOWN 正确 +5分");
                            score += 5;
                        }
                        else
                        {
                            answer.AppendLine("ST01_at_DOWN 错误");
                        }
                        TxObjectList objectNameBylight_sensor = TxApplication.ActiveDocument.GetObjectsByName("light_sensor");
                        if (objectNameBylight_sensor != null)
                        {
                            foreach (ITxObject i in objectNameBylight_sensor)
                            {
                                if ("Tecnomatix.Engineering.TxPlcLightSensor" == i.GetType().ToString())
                                {
                                    var light_sensor = i as TxPlcLightSensor;
                                    try
                                    {
                                        if (light_sensor.ObjectsToCheckInterferenceWith != null && light_sensor.ObjectsToCheckInterferenceWith[0].Name.Equals("YL_CK"))
                                        {
                                            answer.AppendLine("光电传感器检测对象 正确 +5分");
                                            score += 5;
                                        }
                                        else
                                        {
                                            answer.AppendLine("光电传感器检测对象 错误");
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        answer.AppendLine("光电传感器检测对象 错误");
                                    }
                                    try
                                    {
                                        if (light_sensor.MaxRange.Equals(200) && light_sensor.Signal.IECFormatAddress.Equals("I0.3"))
                                        {
                                            answer.AppendLine("光电传感器检测距离，信号配置 正确 +5分");
                                            score += 5;
                                        }
                                        else
                                        {
                                            answer.AppendLine("光电传感器检测距离，信号配置 错误");
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        answer.AppendLine("光电传感器检测距离，信号配置 错误");
                                    }
                                    
                                    
                                }
                            }
                        }
                    }
                }
            }
            else { MessageBox.Show("未找到考核对象"); }
            //---------------------
            Stage.Add("questionId", questionId);
            Stage.Add("littleNo", 3);
            Stage.Add("score", score);
            Stage.Add("answer", answer.ToString());
            questionList.Add(Stage);
        }

        private bool CheckSignalAddressByName(string signalName,string signalAddress)
        {
            TxObjectList objectNameBySignalName = TxApplication.ActiveDocument.GetObjectsByName(signalName);
            bool flag = false;
            if (objectNameBySignalName != null)
            { 
                foreach (ITxObject i in objectNameBySignalName)
                {
                    try
                    {
                        if ("Tecnomatix.Engineering.TxPlcOutputSignal" == i.GetType().ToString())
                        {
                            var ff01stop = i as TxPlcOutputSignal;
                            if (ff01stop != null && ff01stop.IECFormatAddress.Equals(signalAddress))
                            {
                                flag = true;
                            }
                        }
                        if ("Tecnomatix.Engineering.TxPlcInputSignal" == i.GetType().ToString())
                        {
                            var ff01stop = i as TxPlcInputSignal;
                            if (ff01stop != null && ff01stop.IECFormatAddress.Equals(signalAddress))
                            {
                                flag = true;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        flag = false;
                    }
                    
                }
            }
            return flag;
        }
        private void CheckSecondStage(string questionId, List<Dictionary<string, object>> questionList)
        {
            StringBuilder answer = new StringBuilder();
            Dictionary<string, object> Stage = new Dictionary<string, object>();
            double score = 0;
            //---------------------
            
            TxObjectList ojectNameByST01 = TxApplication.ActiveDocument.GetObjectsByName("ST01");
            if (ojectNameByST01 != null)
            {
                foreach (ITxObject item in ojectNameByST01)
                {
                    if ("Tecnomatix.Engineering.TxDevice" == item.GetType().ToString())
                    {
                        TxDevice stopper = item as TxDevice;
                        TxObjectList<TxJoint> joints = stopper.Joints;
                        try
                        {
                            answer.AppendLine(joints[0].Type.ToString());
                        }
                        catch (Exception)
                        {
                            answer.AppendLine("joint数量不对");
                        }
                        
                        //气缸中joint有且仅有一个，并且类型为Prismatic （气缸关节运动方式），否则不给分，若正确则给五分
                        if (joints.Count() == 1 && joints[0].Type.ToString() == "Prismatic")
                        {
                            score += 4;
                            answer.AppendLine("1.运动学关节定义正确");
                            //关节运动限位数值
                            //UpperSoftLimit为10，LowerSoftLimit为0，并且方向正确，z方向from比to大
                            try
                            {
                                answer.AppendLine("upperSoftLimit " + joints[0].UpperSoftLimit.ToString() + '\n' +
                                "LowerSoftLimit" + joints[0].LowerSoftLimit.ToString() + '\n' +
                                "upperHardLimit " + joints[0].HardLimitsData.UpperHardLimit.ToString() + '\n' +
                                "LowerHardLimit" + joints[0].HardLimitsData.LowerHardLimit.ToString() + '\n' +
                                "FZ" + joints[0].Axis.FromPoint.Z.ToString() + "TZ" + joints[0].Axis.ToPoint.Z.ToString() + '\n' +
                                "FX" + joints[0].Axis.FromPoint.X.ToString() + "TX" + joints[0].Axis.ToPoint.X.ToString() +
                                "FY" + joints[0].Axis.FromPoint.Y.ToString() + "TY" + joints[0].Axis.ToPoint.Y.ToString()
                                );
                                if ((joints[0].UpperSoftLimit == 20
                                    && joints[0].LowerSoftLimit == 0)
                                    || (joints[0].HardLimitsData.UpperHardLimit == 20
                                    && joints[0].HardLimitsData.LowerHardLimit == 0)
                                    && (joints[0].Axis.FromPoint.Z > joints[0].Axis.ToPoint.Z
                                    && joints[0].Axis.FromPoint.X == joints[0].Axis.ToPoint.X
                                    && joints[0].Axis.FromPoint.Y == joints[0].Axis.ToPoint.Y))
                                {
                                    score += 4;
                                    answer.AppendLine("2.阻挡气缸限位正确");
                                }
                                else
                                {
                                    answer.AppendLine("阻挡气缸限位错误");
                                }
                            }
                            catch (Exception)
                            {
                                answer.AppendLine("阻挡气缸限位错误");
                            }
                            
                            answer.AppendLine();
                            //验证pose正确性
                            TxObjectList poses = stopper.PoseList;
                            List<string> poseNameList = new List<string>() { "UP", "DOWN" };
                            for (int i = 0; i < poses.Count(); i++)
                            {
                                TxPose pose = poses[i] as TxPose;
                                string name = poseNameList.Find(x => x.Equals(pose.Name));
                                answer.AppendLine(pose.Name + ":");
                                try
                                {
                                    if (name != null && name != "")
                                    {
                                        answer.Append(pose.Name + " " + Convert.ToDouble(pose.PoseData.JointValues[0]).ToString());
                                        if (name.Equals("UP") && Convert.ToDouble(pose.PoseData.JointValues[0]) == 20)
                                        {
                                            poseNameList.Remove("UP");
                                            answer.AppendLine("3. " + pose.Name + " true");
                                            score += 3;
                                        }
                                        else
                                        {
                                            answer.AppendLine("3. " + pose.Name + " false");
                                        }
                                        if (name.Equals("DOWN") && Convert.ToDouble(pose.PoseData.JointValues[0]) == 0)
                                        {
                                            poseNameList.Remove("DOWN");
                                            answer.AppendLine("4. " + pose.Name + " true");
                                            score += 3;
                                        }
                                        else
                                        {
                                            answer.AppendLine("4. " + pose.Name + " false");
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    answer.AppendLine("pose错误");
                                }
                               
                            }
                        }
                        break;
                    }
                }
            }
            else { MessageBox.Show("未找到考核对象"); }
            //---------------------
            Stage.Add("questionId", questionId);
            Stage.Add("littleNo", 2);
            Stage.Add("score", score);
            Stage.Add("answer", answer.ToString());
            questionList.Add(Stage);
        }

        private void CheckFirstStage(string questionId, List<Dictionary<string, object>> questionList)
        {
            StringBuilder answer = new StringBuilder();
            Dictionary<string, object> Stage = new Dictionary<string, object>();
            Double score = 0;
            //---------------------
            //判题逻辑
            TxObjectList ojectNameByDuiduoji = TxApplication.ActiveDocument.GetObjectsByName("duiduoji");
            if (ojectNameByDuiduoji != null)
            {
                foreach (ITxObject item in ojectNameByDuiduoji)
                {
                    if ("Tecnomatix.Engineering.TxDevice" == item.GetType().ToString())
                    {
                        TxDevice duiduoji = item as TxDevice;
                        TxObjectList links = duiduoji.Links;
                        List<string> linkNameList = new List<string>() { "lnk1", "lnk2", "lnk3", "lnk4" };
                        if (links.Count() == 4)
                        {
                            answer.AppendLine("运动学关节分配检测:");
                            try
                            {
                                score += checkLinkAllot(links, linkNameList, answer);
                            }
                            catch (Exception)
                            {
                                answer.AppendLine("link错误");
                            }
                            try
                            {
                                score += checkJointDefine(duiduoji, answer);
                            }
                            catch (Exception)
                            {
                                answer.AppendLine("joint错误");
                            }

                            try
                            {
                                score += checkSignalLogic(duiduoji, answer);
                            }
                            catch (Exception)
                            {
                                answer.AppendLine("signalLogic 错误");
                            }
                           
                        }
                        break;
                    }
                }
            }
            else { MessageBox.Show("未找到考核对象"); }


            //---------------------
            Stage.Add("questionId", questionId);
            Stage.Add("littleNo", 1);
            Stage.Add("score", score);
            Stage.Add("answer", answer.ToString());
            questionList.Add(Stage);
        }
        private double checkSignalLogic(TxDevice duiduoji, StringBuilder answer)
        {
            Double score = 0;
            TxPlcLogicBehavior behavior = duiduoji.LogicBehavior;
            List<string> entrieName = new List<string>() { "X", "Y", "Z" };
            if (behavior.Entries.Count() == 3)
            {
                foreach (ITxPlcLogicBehaviorEntry entrie in behavior.Entries)
                {
                    string en = entrieName.Find(x => x.Equals(entrie.Name));
                    if(en == null) continue;
                    if (entrie.HardwareType == TxPlcHardwareType.Real
                        && entrie.ConnectedSignals.Count() > 0
                        && entrie.ConnectedSignals[0].Name == "duiduoji_"+en
                        )
                    {
                        if ((en.Equals("X") && entrie.ConnectedSignals[0].IECFormatAddress == "Q10")
                            || (en.Equals("Y") && entrie.ConnectedSignals[0].IECFormatAddress == "Q20")
                            || (en.Equals("Z") && entrie.ConnectedSignals[0].IECFormatAddress == "Q30"))
                        {
                            answer.AppendLine(en+ " 信号配置正确 +2.5分");
                            score += 2.5;
                        }
                    }
                }
            }
            if (behavior.Actions.Count() == 3)
            {
                foreach (TxPlcLogicBehaviorMoveJointToValueAction action in behavior.Actions)
                {
                    answer.AppendLine(action.AccelerationExpression.ExpressionString
                       +"/n"+ action.DecelerationExpression.ExpressionString
                        + "/n" + action.TargetVelocityExpression.ExpressionString
                        + "/n" + action.Joint.Name+"/t" + action.ValueExpression.ExpressionString);
                    if(action.AccelerationExpression.ExpressionString.Equals("500")
                        && action.DecelerationExpression.ExpressionString.Equals("500")
                        && action.TargetVelocityExpression.ExpressionString.Equals("500")
                        && action.Joint.Name.Equals(action.ValueExpression.ExpressionString))
                    {
                        answer.AppendLine("速度、加速度、减速度配置正确 +2.5分");
                        score += 2.5;
                    }
                }
            }
                return score;
        }
        private double checkJointDefine(TxDevice duiduoji, StringBuilder answer)
        {
            Double score = 0;


                TxObjectList<TxJoint> joints = duiduoji.Joints;
                if (joints.Count() == 3)
                {
                    string[] upperSoftLimits = { "850", "600", "200" };
                    List<string> resultList = new List<string>();
                    resultList.AddRange(upperSoftLimits);
                    int da = 0;
                    answer.AppendLine("三轴运动学关节定义配置：");
                    for (int i = 0; i < joints.Count(); i++)
                    {
                        TxJoint joint = joints[i];
                        answer.AppendLine("upperSoftLimits:");
                        answer.AppendLine("type = " + joint.Type.ToString());
                        answer.AppendLine(i.ToString());
                        //三个joint都必须为Prismatic并且joint.LowerSoftLimit等于0 否则此joint判错   此处精度仅需到整数
                        if ("Prismatic" != joint.Type.ToString() || !(joint.LowerSoftLimit == 0 || joint.HardLimitsData.LowerHardLimit == 0))
                        {
                            answer.AppendLine("运动方式错误");
                            continue;
                        }
                        //查询UpperSoftLimit是否存在于给定答案中
                        answer.AppendLine("LowerSoftLimit " + joint.LowerSoftLimit);
                        answer.AppendLine("UpperSoftLimit " + joint.UpperSoftLimit.ToString());
                        answer.AppendLine("FX " + Math.Round(joint.Axis.FromPoint.X) + "TX " + Math.Round(joint.Axis.ToPoint.X));
                        answer.AppendLine("FY " + Math.Round(joint.Axis.FromPoint.Y) + "TY " + Math.Round(joint.Axis.ToPoint.Y));
                    answer.AppendLine("FZ " + Math.Round(joint.Axis.FromPoint.Z) + "TZ " + Math.Round(joint.Axis.ToPoint.Z));
                    //查找到对应的轴分别判断
                    string c = resultList.Find(x => { return x.Equals(joint.UpperSoftLimit.ToString())|| x.Equals(joint.HardLimitsData.UpperHardLimit); });
                        if (c == "850"
                            && ((int)Math.Round(joint.Axis.FromPoint.X)) > ((int)Math.Round(joint.Axis.ToPoint.X))
                            && ((int)Math.Round(joint.Axis.FromPoint.Y)) == ((int)Math.Round(joint.Axis.ToPoint.Y))
                            && ((int)Math.Round(joint.Axis.FromPoint.Z)) == ((int)Math.Round(joint.Axis.ToPoint.Z)))
                        {
                            da += 1;
                            answer.AppendLine("5.x轴正确 +2分");
                            resultList.Remove(c);
                        }
                        else if (c == "600"
                            && ((int)Math.Round(joint.Axis.FromPoint.Z)) < ((int)Math.Round(joint.Axis.ToPoint.Z))
                            && ((int)Math.Round(joint.Axis.FromPoint.Y)) == ((int)Math.Round(joint.Axis.ToPoint.Y))
                            && ((int)Math.Round(joint.Axis.FromPoint.X)) == ((int)Math.Round(joint.Axis.ToPoint.X)))
                        {
                            da += 1;
                            answer.AppendLine("6.z轴正确 +2分");
                            resultList.Remove(c);
                        }
                        else if (c == "200"
                            && ((int)Math.Round(joint.Axis.FromPoint.Y)) > ((int)Math.Round(joint.Axis.ToPoint.Y))
                            && ((int)Math.Round(joint.Axis.FromPoint.Z)) == ((int)Math.Round(joint.Axis.ToPoint.Z))
                            && ((int)Math.Round(joint.Axis.FromPoint.X)) == ((int)Math.Round(joint.Axis.ToPoint.X)))
                        {
                            da += 1;
                            answer.AppendLine("7.y轴正确 +2分");
                            resultList.Remove(c);
                        }
                        answer.AppendLine();
                    }
                answer.AppendLine("三轴运动学关节定义配置得" + da*2+"分");
                    score += da * 2;

                }
            return score;
            
        }
        //查看links配置是否正确
        private double checkLinkAllot( TxObjectList links, List<string> linkNameList, StringBuilder answer)
        {
            int case1RightNum = 0;
            for (int i = 0; i < links.Count(); i++)
            {
                TxKinematicLink link = links[i] as TxKinematicLink;
                TxObjectList<ITxKinematicLinkElement> elements = link.Elements;
                string linkName = linkNameList.Find(x => x.Equals(link.Name));
                
                if (linkName == null || linkName == "")
                {
                    answer.AppendLine(link.Name + " 过多或命名不正确");
                    continue;
                }
                if (linkName == "lnk1")
                {

                    string[] mession1ElementsNames = { "152909", "162908" };
                    answer.AppendLine("link1 ");
                    if (linkCheck(link, mession1ElementsNames, answer))
                    {
                        case1RightNum++;
                        answer.AppendLine("Link1运动学关节配置正确 +5分");
                    }
                    else
                    {
                        answer.AppendLine();
                        continue;
                    }
                    answer.AppendLine();
                    linkNameList.Remove("lnk1");
                }
                else if (linkName == "lnk2")
                {
                    answer.AppendLine("link2 ");
                    string[] mession1ElementsNames = { "102912", "52902", "312915", "62901", "202913", "322916", "72900" };
                    if (linkCheck(link, mession1ElementsNames, answer))
                    {
                        case1RightNum++;
                        answer.AppendLine("Link2运动学关节配置正确 +5分");
                    }
                    else { answer.Append('\n'); continue; }
                    answer.AppendLine();
                    linkNameList.Remove("lnk2");
                }
                else if (linkName == "lnk3")
                {
                    answer.AppendLine("link3 ");
                    string[] mession1ElementsNames = { "242907", "392922", "372920", "422925", "382921" };
                    if (linkCheck(link, mession1ElementsNames, answer))
                    {
                        case1RightNum++;
                        answer.AppendLine("Link3运动学关节配置正确 +5分");
                    }
                    else { answer.Append('\n'); continue; }
                    answer.AppendLine();
                    linkNameList.Remove("lnk3");
                }
                else if (linkName == "lnk4")
                {
                    answer.AppendLine("link4 ");
                    string[] mession1ElementsNames = { "252906", "15231" };
                    if (linkCheck(link, mession1ElementsNames, answer))
                    {
                        case1RightNum++;
                        answer.AppendLine("Link4运动学关节配置正确 +5分");
                    }
                    else { answer.Append('\n'); continue; }
                    answer.AppendLine();
                    linkNameList.Remove("lnk4");
                }
            }
            answer.AppendLine("运动学关节分配得"+ case1RightNum * 5);
            return case1RightNum * 5;
        }
        /**
        * 判定link是否正确
        * 入参：
        * link：要判断的link
        * elementNames：其中的element中要全匹配的
        */
        private bool linkCheck(TxKinematicLink link, string[] elementNames,StringBuilder answer)
        {
            TxObjectList<ITxKinematicLinkElement> elements = link.Elements;
            bool flag = false;
            if (elements.Count() == elementNames.Count())
            {
                List<string> resultList = new List<string>();
                resultList.AddRange(elementNames);
                for (int j = 0; j < elements.Count(); j++)
                {
                    answer.Append(elements[j].Name+" ");
                    string c = resultList.Find(x => x.Equals(elements[j].Name));
                    //查找出对应的Name 若查找不出，进行下一个link的查询
                    if (c == null || c == "")
                    {
                        continue;
                    }
                    resultList.Remove(c);
                }
                if (resultList.Count == 0)
                {
                    flag = true;
                }
            }
            return flag;
        }
        public ITxObject SerchObject(string ObjName)
        {
            TxObjectList ObjectByName = TxApplication.ActiveDocument.GetObjectsByName(ObjName);
            if (ObjectByName == null)
            {
                return null;
            }
            else
            {
                foreach (ITxObject item in ObjectByName)
                {
                    return item;
                }
                return null;
            }
        }
    }
}
